<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class IssueController extends Controller
{

    public function index(Request $request)
    {	
        // dd($request);
        // dd($id);
        // session中的homeuser如果存在直接进入购物车页否则显示购物车登陆页面
    	if ($request->session()->has('homeuser')) {
            $id = $request->session()->get('homeuser')->userid;
            $username = $request->session()->get('homeuser')->username;
    		$list = DB::table('issue')->where('userid','=',$id)->get();
            // dd($list);
            if ($list== 'items: []') {
                // return view('home.issue.indexnull');
            }else{
                return view('home.issue.index',['list'=>$list,'username'=>$username]);
            }
    	}else{
            return view('home.issue.issuelogin');
    	}
    }
    // 处理问题反馈的登陆页面
    public function store(Request $request)
    {
         $name = $request->input("user");
        // $pass = $request->input("pwd");
        $ob = DB::table("user")->where("username",$name)->first();
        if(empty($name)){
            return back()->with("msg","请输入账号");
        }
        if($ob){
            if($request->input("pwd")==$ob->userpassword){
                session(["homeuser"=>$ob]);
                // 登陆成功进入购物车页
                return redirect("/home/issue");
            }else{
                // return back()->with("msg","登录失败：密码错误");
            }
        }else{
            return back()->with("msg","登录失败：账号不存在");
        }
    }
   	public function create()
    {
    	return view('home.issue.index');
    }
    public function storemit(Request $request)
    {
        if ($request->input('phone')=="" || $request->input('content')=="") {
            return back()->with("msg","联系方式或问题必须填写");
        }else{
            $username = $request->input('username');
            $user = DB::table('user')->where('username','=',$username)->get();
            $userid = $user[0]->userid;
            $content = $request->input('content');
            $phone = $request->input('phone');
            $data = array('userid'=>$userid,'content'=>$content,'phone'=>$phone);
            $m = DB::table('issue')->insertGetId($data);
            if($m>0){
                return redirect('home/issue');
            }
        }
    }
    // 用户的删除问题操作
    public function show($id)
    { 
        DB::table('issue')->where('issueid',$id)->delete();
        return redirect('/home/issue');
    }
    public function edit($id)
    {
        $list = DB::table('issue')->where('issueid',$id)->first();
        return view('home.issue.edit',['list'=>$list]);
    }
    public function update($id,Request $request)
    {
        $data = $request->only('content','phone');
        DB::table('issue')->where('issueid',$id)->update($data);
        return redirect('home/issue');
    }
}
